from django import template
from django import newforms as forms
from blog.models import Blog, Category, Entry
from admin.models import EntryCategoryForm

register = template.Library()

@register.inclusion_tag('admin/tags/_field.html')
def display_field(field, alt_label=''):
  if alt_label: field.label = alt_label
  label_first = not isinstance(field.field.widget, forms.widgets.CheckboxInput)
  return { 'field': field, 'label_first': label_first }

@register.inclusion_tag('admin/tags/_field_label.html')
def display_field_label(field):
  return { 'field': field }

@register.inclusion_tag('admin/tags/_entry_categories.html', takes_context=True)
def display_entry_categories(context):
  blog = context['blog']
  entry = 'entry' in context and context['entry'] or None
  category_forms = context['category_forms'] or [EntryCategoryForm(category, entry=entry) for category in blog.categories]
  return { 'category_forms': category_forms }